﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ManageTicket.Library.Common
{
    public class CommonFunction
    {
        public void GetPermission(ref bool bAdd, ref bool bEdit, ref bool bDelete, ref bool bCancel, ref bool bSave, ref bool bFunction, ref bool bView, ref bool bFind, ref bool bExport, ref bool bSendmail, ref bool bPrint, string user, string formname)
        {
            //
            Business.UsersRole boUR = new Business.UsersRole();
            Business.Permission boPer = new Business.Permission();
            ArrayList arrUR = boUR.GetListByUserId(user);
            DataObjects.Permission oPer;
            if (arrUR.Count > 0)
            {
                foreach (DataObjects.UsersRole oUR in arrUR)
                {
                    oPer = boPer.GetObjectByPK(oUR.GroupId, formname);
                    if (oPer != null)
                    {
                        bAdd = bAdd || oPer.FuncAdd.Value;
                        bEdit = bEdit || oPer.FuncEdit.Value;
                        bDelete = bDelete || oPer.FuncDelete.Value;
                        bCancel = bCancel || oPer.FuncCancel.Value;
                        bSave = bSave || oPer.FuncSave.Value;
                        bFunction = bFunction || oPer.FuncMore.Value;
                        bView = bView || oPer.FuncView.Value;
                        bFind = bFind || oPer.FuncFind.Value;
                        bExport = bExport || oPer.FuncExport.Value;
                        bSendmail = bSendmail || oPer.FuncSendmail.Value;
                        bPrint = bPrint || oPer.FuncPrint.Value;
                    }
                }
            }
        }

        public void GetPermissionReport(ref bool bAllow, string user, string formname)
        {
            //
            Business.UsersRole boUR = new Business.UsersRole();
            Business.PermissionReport boPer = new Business.PermissionReport();
            ArrayList arrUR = boUR.GetListByUserId(user);
            DataObjects.PermissionReport oPer;
            if (arrUR.Count > 0)
            {
                foreach (DataObjects.UsersRole oUR in arrUR)
                {
                    oPer = boPer.GetObjectByPK(oUR.GroupId, formname);
                    if (oPer != null)
                    {
                        bAllow = bAllow || oPer.FuncAllow.Value;
                    }
                }
            }
        }

        public string RuleNumber5(int x)
        {
            string str = x.ToString();
            if (x < 10)
            {
                str = "0000" + x.ToString();
            }
            else if (x >= 10 && x < 100)
            {
                str = "000" + x.ToString();
            }
            else if (x >= 100 && x < 1000)
            {
                str = "00" + x.ToString();
            }
            else if (x >= 1000 && x < 10000)
            {
                str = "0" + x.ToString();
            }
            
            return str;
        }

        public void GetListCodeType(System.Windows.Forms.ComboBox cbo, string sGroup)
        {
            try
            {
                cbo.DataSource = null;
                Business.CodeType boCodeType = new Business.CodeType();
                ArrayList arr = boCodeType.GetListCodeTypeByGroup(sGroup);
                cbo.DataSource = arr;
                cbo.ValueMember = "CodeTypeId";
                cbo.DisplayMember = "CodeTypeName";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void GetListCodeType(System.Windows.Forms.ComboBox cbo, string sGroup, DataObjects.CodeType first_item)
        {
            try
            {
                cbo.Items.Add(first_item);

                Business.CodeType boCodeType = new Business.CodeType();
                ArrayList arr = boCodeType.GetListCodeTypeByGroup(sGroup);
                if (arr != null && arr.Count > 0)
                {
                    foreach (DataObjects.CodeType oCodeType in arr)
                    {
                        cbo.Items.Add(oCodeType);
                    }
                }

                cbo.ValueMember = "CodeTypeId";
                cbo.DisplayMember = "CodeTypeName";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public string GetConfig(string key)
        {
            try
            {
                return ConfigurationSettings.AppSettings[key].ToString();
            }
            catch (Exception ex)
            {
                return "";
                //MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void SetConfig(string key, string value)
        {
            try
            {
                ConfigurationSettings.AppSettings.Set(key, value);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddListVoucher(DataObjects.Voucher oVou)
        {
            if (Common.CommonList.ListVoucher == null)
            {
                Common.CommonList.ListVoucher = new ArrayList();
            }
            bool IsExist = false;
            foreach (DataObjects.Voucher oVou_ in Common.CommonList.ListVoucher)
            {
                if (oVou_.VoucherNo == oVou.VoucherNo)
                {
                    IsExist = true;
                    break;
                }
            }
            if (IsExist == false)
            {
                Common.CommonList.ListVoucher.Add(oVou);
            }
        }

        public void RemoveListVoucher(DataObjects.Voucher oVou)
        {
            if (Common.CommonList.ListVoucher != null)
            {
                foreach (DataObjects.Voucher oVou_ in Common.CommonList.ListVoucher)
                {
                    if (oVou_.VoucherNo == oVou.VoucherNo)
                    {
                        Common.CommonList.ListVoucher.Remove(oVou_);
                        break;
                    }
                }
            }
        }
    }

    public class ObjectInfo
    {
        private string sCode;
        private string sName;
        public string Code
        {
            get { return sCode; }
            set { sCode = value; }
        }
        public string Name
        {
            get { return sName; }
            set { sName = value; }
        }
    }

    public class CommonList
    {
        public static ArrayList ListVoucher;
    }
}
